#include "JLogWidget.h"

#include <QHeaderView>

JLogWidget::JLogWidget(QWidget *parent)
	: QTreeWidget(parent),
	m_iEntryCount(0)
{
	//setObjectName(QString::fromUtf8("JLogWidget"));
	setMouseTracking(true);
	//setFrameShape(QFrame::NoFrame);
	//setFrameShadow(QFrame::Plain);
	setAlternatingRowColors(true);
	setIndentation(11);
	setAnimated(true);
	setUniformRowHeights(true);
	setIconSize(QSize(16,16));

	QTreeWidgetItem *mLogHeader = headerItem();
	mLogHeader->setText(3, tr(""));
	mLogHeader->setText(2, tr("Message"));
	mLogHeader->setText(1, tr("Time"));
	mLogHeader->setText(0, tr("Type"));
	//header()->setResizeMode(3,QHeaderView::Fixed);
	header()->setResizeMode(2,QHeaderView::ResizeToContents);
	//header()->resizeSection(2,600);
	header()->setResizeMode(1,QHeaderView::ResizeToContents);
	header()->setResizeMode(0,QHeaderView::ResizeToContents);

	QIcon icon;
	icon.addFile(QString::fromUtf8(":/JLog/Resources/accept.png"), QSize(), QIcon::Normal, QIcon::Off);
	QIcon icon1;
	icon1.addFile(QString::fromUtf8(":/JLog/Resources/info.png"), QSize(), QIcon::Normal, QIcon::Off);
	QIcon icon2;
	icon2.addFile(QString::fromUtf8(":/JLog/Resources/warning.png"), QSize(), QIcon::Normal, QIcon::Off);
	QIcon icon3;
	icon3.addFile(QString::fromUtf8(":/JLog/Resources/critical.png"), QSize(), QIcon::Normal, QIcon::Off);
	QIcon icon4;
	icon4.addFile(QString::fromUtf8(":/JLog/Resources/critical.png"), QSize(), QIcon::Normal, QIcon::Off);
	m_IconMap.insert(JLOG_SUCCESS, icon);
	m_IconMap.insert(JLOG_INFO, icon1);
	m_IconMap.insert(JLOG_WARNING, icon2);
	m_IconMap.insert(JLOG_ERROR, icon3);
	m_IconMap.insert(JLOG_CRITICAL, icon4);

	connect(&JLog::Instance(),SIGNAL(sendPost(JLogEntry*)),this,SLOT(updateLog2(JLogEntry*)));
	connect(&Log, SIGNAL(sendList(QList<JLogEntry>*)), this, SLOT(updateLog(QList<JLogEntry>*)));
}

JLogWidget::~JLogWidget()
{

}

void JLogWidget::updateLog(QList<JLogEntry> *logEntryList)
{
	m_pTopItem = new QTreeWidgetItem();
	m_pTopItem->setIcon(0,m_IconMap[logEntryList->last().mType]);
	m_pTopItem->setText(0,tr(" "));
	m_pTopItem->setText(1, logEntryList->last().mTime+tr(" "));
	m_pTopItem->setText(2, logEntryList->last().mMsg+tr(" "));
	m_pTopItem->setFlags(m_pTopItem->flags()|Qt::ItemIsEditable);
	addTopLevelItem(m_pTopItem);

	for (int ii = 0; ii < logEntryList->size()-1; ++ii)
	{
		m_pItem = new QTreeWidgetItem(m_pTopItem);
		m_pItem->setIcon(0,m_IconMap[logEntryList->at(ii).mType]);
		m_pItem->setText(0,tr(" "));
		m_pItem->setText(1,logEntryList->at(ii).mTime+tr(" "));
		m_pItem->setText(2,logEntryList->at(ii).mMsg+tr(" "));
		m_pItem->setFlags(m_pItem->flags()|Qt::ItemIsEditable);
		addTopLevelItem(m_pItem);
	}
	scrollToItem(m_pTopItem);
	if (logEntryList->last().mType == JLOG_ERROR)
		expandItem(m_pTopItem);
	m_iEntryCount++;
}